//
//  HMEmoticonPageCell.swift
//  表情键盘
//
//  Created by iOS on 16/9/26.
//  Copyright © 2016年 iOS. All rights reserved.
//

import UIKit

/// 1.定义协议
protocol HMEmoticonPageCellDelegate: NSObjectProtocol {
    // 2.定义方法
    /// 表情按钮被点击了
    func emoticonPageCellDidClickEmoticonButton(emoticonModel model: HMEmoticonModel) -> Void
    
    /// 删除按钮点击了
    func emoticonPageCellDidClickDeleteButton() -> Void
}

/// 一页20个表情按钮
let NumberOfCell = 20

/// 7列
let ColumnOfCell = 7

/// 3行
let RowOfCell = 3

// cell
class HMEmoticonPageCell: UICollectionViewCell {
    
    /// 3.定义代理属性
    weak var delegate: HMEmoticonPageCellDelegate?
    
    /// 表情按钮
    private var emoticonButtons: [HMEmoticonButton] = [HMEmoticonButton]()
    
    var indexPath: IndexPath? {
        didSet {
            debugLabel.text = "第 \(indexPath!.section) 组, 第 \(indexPath!.item) 个Cell"
            
            // 第0组显示,其他组隐藏
            recentLabel.isHidden = indexPath!.section != 0
        }
    }
    
    /// 一页对应的表情模型,最多20个
    var pageEmoticon: [HMEmoticonModel]? {
        didSet {
            guard let pageEmoticon = pageEmoticon else {
                print("cell没有模型")
                return
            }
            
            // 隐藏所有按钮
            for button in emoticonButtons {
                button.isHidden = true
            }
            
            // 获取到要显示的模型数据
            for (index, emoticon) in pageEmoticon.enumerated() {
                // 找到对应的按钮
                let emoticonButton = self.emoticonButtons[index]
                // 获取到每一个模型,将模型赋值给按钮,让按钮去显示
                
                emoticonButton.emoticon = emoticon
                emoticonButton.isHidden = false
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addEmoticonButtons()
        
        // 删除按钮
        contentView.addSubview(deleteButton)
        
        // 添加测试label
        contentView.addSubview(debugLabel)
        contentView.addSubview(recentLabel)
    }
    
    /// 添加20个表情按钮
    private func addEmoticonButtons() {
        for _ in 0..<NumberOfCell {
            let button = HMEmoticonButton()
//            button.backgroundColor = UIColor.random
            // 设置标题大小
            button.titleLabel?.font = UIFont.systemFont(ofSize: 32)
            
            button.addTarget(self, action: #selector(emoticonButtonDidClick(button:)), for: .touchUpInside)
            
            emoticonButtons.append(button)
            contentView.addSubview(button)
        }
    }
    
    // MARK: - 点击事件
    func deleteButtonDidClick() -> Void {
        // 4.调用代理方法
        delegate?.emoticonPageCellDidClickDeleteButton()
    }
    
    /// 按钮点击事件
    ///
    /// - parameter button: 按钮
    func emoticonButtonDidClick(button: HMEmoticonButton) -> Void {
        // 4.调用代理方法
        delegate?.emoticonPageCellDidClickEmoticonButton(emoticonModel: button.emoticon!)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        /// 左右间距
        let marginLR: CGFloat = 5
        /// 底部间距 20
        let bottomMargin: CGFloat = 20
        
        /// 一个按钮宽
        let width = (self.frame.width - 2 * marginLR) / CGFloat(ColumnOfCell)
        /// 一个按钮高度
        let height = (self.frame.height - bottomMargin) / CGFloat(RowOfCell)
        
        for (index, button) in emoticonButtons.enumerated() {
            // 在哪列
            let column = index % ColumnOfCell
            // 在哪行
            let row = index / ColumnOfCell
            
            /// 计算按钮的frame
            /// x
            let x = marginLR + CGFloat(column) * width
            /// y
            let y = CGFloat(row) * height
            
            button.frame = CGRect(x: x, y: y, width: width, height: height)
        }
        
        // 删除按钮的frame
        let x = marginLR + CGFloat(ColumnOfCell - 1) * width
        let y = CGFloat(RowOfCell - 1) * height
        deleteButton.frame = CGRect(x: x, y: y, width: width, height: height)
        
        // 最近label
        recentLabel.center = CGPoint(x: self.frame.width * 0.5, y: self.frame.height - recentLabel.bounds.height + 3)
    }
    
    private lazy var debugLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.font = UIFont.systemFont(ofSize: 18)
        label.frame = CGRect(x: 0, y: 0, width: 300, height: 100)
        
        return label
    }()
    
    /// 最新使用label
    private lazy var recentLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.text = "最近使用表情"
        label.textColor = UIColor.lightGray
        label.font = UIFont.systemFont(ofSize: 13)
        label.sizeToFit()
        
        return label
    }()
    
    /// 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: .highlighted)
        button.addTarget(self, action: #selector(deleteButtonDidClick), for: .touchUpInside)
        
        return button
    }()
}
